//
//  ZH_FeedbackTableViewCell.m
//  HenticaSample
//
//  Created by zhangliwen on 2018/5/3.
//  Copyright © 2018年 HenticaSample. All rights reserved.
//

#import "ZH_FeedbackTableViewCell.h"
@interface ZH_FeedbackTableViewCell()<UITextFieldDelegate,UITextViewDelegate>
///输入框
@property (nonatomic, weak) UITextField *textField;
///备注
@property (nonatomic,weak)UITextView *inputTextView;
///备注的说明
@property (nonatomic,weak)UILabel *instructionsLabel;

///提示
@property (nonatomic, weak) UILabel *wordNumDesLabel;
@end
@implementation ZH_FeedbackTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_SetUpTableViewCell";
    ZH_FeedbackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_FeedbackTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
}
- (void)loadSubviewConstraints{
    
    [self.inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.top.equalTo(self.contentView);
        make.height.mas_equalTo(FITSCALE(160));
        make.bottom.equalTo(self.contentView);
    }];
    [self.instructionsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.inputTextView).offset(FITHEIGHT(7));
        make.left.equalTo(self.inputTextView).offset(FITWITH(10));
    }];
//    [self.wordNumDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.inputTextView);
//        make.top.equalTo(self.inputTextView.mas_bottom);
//        make.bottom.equalTo(self.contentView);
//    }];
}
#pragma mark -- UITextFeildDelegate
///输入框 回调 方法
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(self.onEnterTextFieldBlock){
        self.onEnterTextFieldBlock(textField.text, textField.tag);
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textField endEditing:YES];
   
    return YES;
}
///键盘类型
-(void)setTextFieldForKeyboardType:(UIKeyboardType)keyboardType
{
    self.textField.keyboardType = keyboardType;
    
}
///输入框 回调 方法
- (void)textViewDidEndEditing:(UITextView *)textView{
    
    if (self.onInputTextViewBlock) {
        self.onInputTextViewBlock(textView.text);
    }
}
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        _instructionsLabel.hidden = NO;
    }
    else {
        _instructionsLabel.hidden = YES;
    }
    if (textView.markedTextRange == nil && textView.text.length >= (long)self.maxCount) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, self.maxCount)];
    }
    NSString *textStr = [NSString stringWithFormat:@"%ld/%ld", (unsigned long)textView.text.length, (long)self.maxCount];
    self.wordNumDesLabel.text = textStr;

}
//隐藏键盘，实现UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

{
    if (textView.text.length >= self.maxCount && text.length > range.length)
    {
        return NO;
    }
    return YES;
}
- (UITextField *)textField
{
    if(!_textField){
        UITextField *textField = [UITextField createTextFieldWithPlaceholder:@"" placeholderColor:kFontColorGray delegateTarget:self];
        textField.borderStyle = UITextBorderStyleNone;
        textField.font = kFontSize_28;
        textField.textAlignment = NSTextAlignmentLeft;
        textField.returnKeyType = UIReturnKeyDone;
        textField.tag = 1;
        textField.layer.borderWidth = 0.5f;
        textField.layer.borderColor = [UIColor colorWithHexString:@"#EEEEEE"].CGColor;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [textField setTextColor:kFontColorBlack];
        [self.contentView addSubview:_textField = textField];
    }
    return _textField;
}
///输入内容
- (UITextView *)inputTextView
{
    if(!_inputTextView){
        UITextView *textView= [UITextView createTextViewWithDelegateTarget:self];
        textView.font = kFontSize_26;
        [self.contentView addSubview:_inputTextView = textView];
    }
    return _inputTextView;
}
- (UILabel *)instructionsLabel{
    if (!_instructionsLabel) {
        UILabel *label = [UILabel createLabelWithText:@"请输入投诉内容..." font:kFontSize_26];
        label.textColor = [UIColor colorWithHexString:@"#969696"];
        [self.contentView addSubview:_instructionsLabel = label];
    }
    return _instructionsLabel;
}
- (UILabel *)wordNumDesLabel
{
    if(!_wordNumDesLabel){
        UILabel *label = [UILabel createLabelWithText:@"0/250" font:kFontSize_28];
        [self.contentView addSubview:_wordNumDesLabel = label];
    }
    return _wordNumDesLabel;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    self.textField.placeholder = placeholder;
}

- (void)setInputText:(NSString *)inputText
{
    _inputText = inputText;
    self.textField.text = inputText;
}

@end
